package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"time"

	"github.com/cavaliercoder/grab"
)

func main() {
	fmt.Println("下载网络文件到本地文件夹")
	paramlist := os.Args
	paramSize := len(paramlist)
	if paramSize != 3 {
		if paramSize == 2 {
			var params [2]string
			for n, p := range paramlist {
				params[n] = p
				fmt.Println("n = ", n, ";param = ", params[n])
			}
			time.Sleep(time.Duration(2) * time.Second) // sleep 2 second
			cmd := exec.Command("cmd.exe", "/c", "start "+params[1])
			err := cmd.Run()
			if err != nil {
				log.Println("启动失败:", err)
			} else {
				log.Println("启动成功!")
			}
		}
		//return
	}
	var params [3]string
	for n, p := range paramlist {
		params[n] = p
		fmt.Println("n = ", n, ";param = ", params[n])
	}

	/// 2 delete // 确实实时状态回传
ReDownload:
	resp, err := grab.Get("C:/Users/Administrator/go/src/gitee.com/xuanwuyan/go-download-windows", "https://statichz.qiuhui.com/avatar/e16b0ac4-1677-4595-8aee-b7e5a6a15d94.exe")
	// unexpected EOF
	if err != nil {
		//log.Fatal(err)
		log.Println(err)
		goto ReDownload
	} else {
		fmt.Println("Download saved to", resp.Filename)
	}
}
